import os
import sys
import configs
import time

from typing import List
from alibabacloud_alidns20150109.client import Client as Alidns20150109Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_alidns20150109 import models as alidns_20150109_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient

class ChangeDns:
    def __init__(self):
        pass

    @staticmethod
    def create_client() -> Alidns20150109Client:
        """
        使用AK&SK初始化账号Client
        @return: Client
        @throws Exception
        """
        # 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        # 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378659.html。
        config = open_api_models.Config(
            # 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。,
            access_key_id=configs.AccessKey_ID,
            # 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。,
            access_key_secret=configs.AccessKey_Secret
        )
        # Endpoint 请参考 https://api.aliyun.com/product/Alidns
        config.endpoint = f'alidns.cn-beijing.aliyuncs.com'
        return Alidns20150109Client(config)

    @staticmethod
    def DomainInfo() -> None:
        client = ChangeDns.create_client()
        describe_domain_info_request = alidns_20150109_models.DescribeDomainInfoRequest(
            domain_name=configs.domain
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            return client.describe_domain_info_with_options(describe_domain_info_request, runtime).body
        except Exception as error:
            print(error.message)
            time.sleep(3)


    @staticmethod
    def ListRecord() -> None:
        client = ChangeDns.create_client()
        describe_domain_records_request = alidns_20150109_models.DescribeDomainRecordsRequest(
            domain_name=configs.domain
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            return client.describe_domain_records_with_options(describe_domain_records_request, runtime).body.domain_records.record
        except Exception as error:
            print(error.message)
            time.sleep(3)

    @staticmethod
    def AddRecord(RecordName:str,IP:str):
        client = ChangeDns.create_client()
        add_domain_record_request = alidns_20150109_models.AddDomainRecordRequest(
            domain_name=configs.domain,
            rr=RecordName,
            type='A',
            value=IP
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.add_domain_record_with_options(add_domain_record_request, runtime)
            print('\nadd success....')
            time.sleep(3)
        except Exception as error:
            print(error.message)
            print('an exception occurs.Please check the input......')
            time.sleep(3)


    @staticmethod
    def ModifyRecord(
            RecordId,RecordName:str,IP:str
    ) -> None:
        client = ChangeDns.create_client()
        update_domain_record_request = alidns_20150109_models.UpdateDomainRecordRequest(
            record_id=RecordId,
            rr=RecordName,
            type='A',
            value=IP
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.update_domain_record_with_options(update_domain_record_request, runtime)
            print('\nmodify success....')
            time.sleep(3)

        except Exception as error:
            print(error.message)
            print('an exception occurs.Please check the input......')

    @staticmethod
    def DeleteRecord(
            recordId
    ) -> None:
        client = ChangeDns.create_client()
        delete_domain_record_request = alidns_20150109_models.DeleteDomainRecordRequest(
            record_id=recordId
        )
        runtime = util_models.RuntimeOptions()
        try:
            # 复制代码运行请自行打印 API 的返回值
            client.delete_domain_record_with_options(delete_domain_record_request, runtime)
            print('\ndelete success.....')
            time.sleep(3)

        except Exception as error:
            # 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            # 错误 message
            print(error.message)
            print('an exception occurs.Please check the input......')

